<?php
 class Pictures extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
     /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getList(){
        $this->db->select();
        $this->db->from('pictures');
        $this->db->join('products', 'products.ProductId = pictures.ProductId', 'left');
        $this->db->order_by('pictures.PictureId desc');
        $query = $this->db->get();
        $res = $query->result_array();
        return $res;
    }
    
    /**
     * Add customer
     * @author tien.nguyen 
     */
    public function add($data){
        //$data['CreatedDate'] = date('Y-m-d H:m:s');
        unset($data['PictureId']);
        return $this->db->insert('pictures', $data);
    }
    
       /**
     * edit function in config
     * @author tien.nguyen
     */
    public function edit($data, $awardId){
        //echo '<pre>'; print_r($data); die;
        $data = $this->_convertData($data);
        return $this->db->update('awards', $data, array('AwardId' => $awardId));
    }
    
     
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($pictureId){
        return $this->db->delete('pictures', array('PictureId' => $pictureId));
    }
    
      /**
     * Get product by product id
     * @author tien.nguyen
     */
    public function getPicture($awardId){
        $query = $this->db->get_where('awards', array('AwardId' => $awardId));
        return $query->row_array();
    }
    
     /**
     * Convert data array to array of product
     * @author tien.nguyen
     */
    private function _convertData($data){
        $res = array();
        $arr = array('PictureId', 'PictureName', 'PictureLink', 
            'ProductId', 'Visible');
        foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
        }
        
        //set default value
        $res['Visible'] = isset($res['Visible']) ? $res['Visible'] : 0;
        return $res;
    }
    
 }